define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'management/index',
                    add_url: 'management/add',
                    edit_url: 'management/edit',
                    del_url: 'management/del',
                    import_url: 'management/import',
                    imports_url: 'management/imports',
                    yz_url: 'management/importYz',//有赞导入
                    tb_url: 'management/importTb',//淘宝导入
                    jd_url: 'management/importJd',//京东导入
                    wd_url: 'management/importWd',//微店导入
                    sq_url: 'management/importSq',//社群订单导入
                    jl_url: 'management/importJl',//记录订单导入
                    sc_url: 'management/importsc',//上册订单导入
                    multi_url: 'management/multi',
                    table: 'management',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),visible:true,sortable: true},
                        {field: 'orderid', title: __('Orderid')},
                        {field: 'source', title: __('Source'),searchList:  $.getJSON('ajax/ManSelect'), formatter: Table.api.formatter.normal},
                        {field: 'goods', title: __('Goods')},
                        {field: 'version', title: __('Version'),visible:true},
                        {field: 'gifts', title: __('Gifts'),visible:false},
                        {field: 'num', title: __('Num')},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'consignee', title: __('Consignee')},
                        {field: 'consigneePhone', title: __('Consigneephone')},
                        {field: 'consigneeAddress', title: __('Consigneeaddress')},
                        {field: 'deliveryForm', title: __('Deliveryform'),visible:true},
                        {field: 'courierBut', title: __('Courierbut'),visible:true},
                        {field: 'orderStatus', title: __('订单状态'),searchList: {"已发货":__('已发货'),"未发货":__('未发货')},visible:true},
                        {field: 'send', title: __('短信次数'),visible:true,formatter: Controller.api.formatter.Send,events: Controller.api.events.send,operate: false,},
                        {field: 'gift', title: __('Gift'),searchList: {"1":__('Gift 1'),"2":__('Gift 2')},events: Controller.api.events.GiftStatus,formatter: Controller.api.formatter.Gift},
                        {field: 'pay_nb', title: __('Pay_nb'),visible:false},
                        {field: 'storageMedium', title: __('Storagemedium'),visible:false},
                        {field: 'courierFees', title: __('Courierfees'), operate:'BETWEEN',visible:false},
                        {field: 'salesman', title: __('Salesman'),visible:false},
                        {field: 'delivery', title: __('Delivery'),visible:false},
                        {field: 'wechat', title: __('Wechat'),visible:false},
                        {field: 'alipay', title: __('Alipay'),visible:false},
                        {field: 'cash', title: __('Cash'),visible:false},
                        {field: 'pos', title: __('Pos'),visible:false},
                        {field: 'reseller', title: __('代理商店铺'),visible:false},
                        {field: 'resellerPhone', title: __('代理商手机号'),visible:false},
                        {field: 'payment', title: __('Payment'),visible:false},
                        {field: 'remake', title: __('Remake'),visible:true},
                        {field: 'user_text', title: __('导入人')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false,sortable: true},
                        {field: 'ordertime', title: __('下单时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'paytime', title: __('支付时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //导入
            require(['upload'], function (Upload) {
              Upload.api.plupload($('.btn-import-tb'), function (data, ret) {
                Fast.api.ajax({
                  url:$.fn.bootstrapTable.defaults.extend.tb_url ,
                  data: {file: data.url},
                }, function (data, ret) {
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-import-jd'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.jd_url,
                  data: {file: data.url},
                }, function (data, ret) {
                  if(data.length >= 1){
                    var htmlstr = '';
                    data.forEach(function (element, index, array) {
                      let orderid = array[index].orderid;
                      htmlstr +='<li>订单号：'+orderid+' </li>';
                    });
                    layer.open({
                      type: 1,
                      title: '重复数据的订单号',
                      skin: 'layui-layer-demo', //样式类名
                      closeBtn: 0, //不显示关闭按钮
                      anim: 2,
                      shadeClose: true, //开启遮罩关闭
                      content: '<ul>'+htmlstr+'</ul>'
                    });
                  }
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-import-wd'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.wd_url,
                  data: {file: data.url},
                }, function (data, ret) {
                 if(data.length >= 1){
                   var htmlstr = '';
                   data.forEach(function (element, index, array) {
                     let orderid = array[index].orderid;
                     htmlstr +='<li>订单号：'+orderid+' </li>';
                   });
                   layer.open({
                     type: 1,
                     title: '重复数据的订单号',
                     skin: 'layui-layer-demo', //样式类名
                     closeBtn: 1, //不显示关闭按钮
                     anim: 2,
                     shadeClose: true, //开启遮罩关闭
                     content: '<ul>'+htmlstr+'</ul>'
                   });
                 }
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-import-yz'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.yz_url,
                  data: {file: data.url},
                }, function (data, ret) {
                  if(data.length >= 1){
                    var htmlstr = '';
                    data.forEach(function (element, index, array) {
                      let orderid = array[index].orderid;
                      htmlstr +='<li>订单号：'+orderid+' </li>';
                    });
                    layer.open({
                      type: 1,
                      title: '重复数据的订单号',
                      skin: 'layui-layer-demo', //样式类名
                      closeBtn: 1, //不显示关闭按钮
                      anim: 2,
                      shadeClose: true, //开启遮罩关闭
                      content: '<ul>'+htmlstr+'</ul>'
                    });
                  }
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-import-sq'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.sq_url,
                  data: {file: data.url},
                }, function (data, ret) {
                  if(data.length >= 1){
                    var htmlstr = '';
                    data.forEach(function (element, index, array) {
                      let orderid = array[index].orderid;
                      htmlstr +='<li>订单号：'+orderid+' </li>';
                    });
                    layer.open({
                      type: 1,
                      title: '重复数据的订单号',
                      skin: 'layui-layer-demo', //样式类名
                      closeBtn: 1, //不显示关闭按钮
                      anim: 2,
                      shadeClose: true, //开启遮罩关闭
                      content: '<ul>'+htmlstr+'</ul>'
                    });
                  }
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-import-jl'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.jl_url,
                  data: {file: data.url},
                }, function (data, ret) {
                  table.bootstrapTable('refresh');
                });
              });
              Upload.api.plupload($('.btn-imports'), function (data, ret) {
                Fast.api.ajax({
                  url: $.fn.bootstrapTable.defaults.extend.imports_url,
                  data: {file: data.url},
                }, function (data, ret) {
                  if(data.length >= 1){
                    var htmlstr = '';
                    data.forEach(function (element, index, array) {
                      htmlstr +='<li>'+array[index]+' </li>';
                    });
                    layer.open({
                      type: 1,
                      title: '错误的数据',
                      skin: 'layui-layer-demo', //样式类名
                      closeBtn: 1, //不显示关闭按钮
                      anim: 2,
                      shadeClose: true, //开启遮罩关闭
                      content: '<ul>'+htmlstr+'</ul>'
                    });
                  }
                  table.bootstrapTable('refresh');
                });
              });

                Upload.api.plupload($('.btn-import-sc'), function (data, ret) {
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.sc_url,
                        data: {file: data.url},
                    }, function (data, ret) {
                        if(data.length >= 1){
                            var htmlstr = '';
                            data.forEach(function (element, index, array) {
                                htmlstr +='<li>'+array[index]+' </li>';
                            });
                            layer.open({
                                type: 1,
                                title: '错误的数据',
                                skin: 'layui-layer-demo', //样式类名
                                closeBtn: 1, //不显示关闭按钮
                                anim: 2,
                                shadeClose: true, //开启遮罩关闭
                                content: '<ul>'+htmlstr+'</ul>'
                            });
                        }
                        table.bootstrapTable('refresh');
                    });
                });

            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
          formatter: {//渲染的方法
            Gift: function (value, row, index) {
                 var color = 'success';
                  if(value == 1){
                    color = 'yellow';
                   return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.gift_text+'</span>';
                  }
                  if(value == 2){
                       return '<a class="btn btn-xs btn-dialog btn-gift"><span class="text-'+color+'"><i class="fa fa-circle"></i> '+row.gift_text+'</span> </a>';
                   }
                },
            Send: function (value, row, index) {
              return '<a class="btn btn-xs btn-dialog btn-gift"><span class="text-success"><i class="fa fa-circle"></i> '+row.send_text+'</span> </a>';
            }
          },
           events: {//绑定事件的方法
                 GiftStatus: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-gift': function (e, value, row, index) {
                      console.log(row);
                        e.stopPropagation();
                        var str = '';
                        if(row.userid){
                           str += "微信昵称: <code>" +row.username_text + "</code><br>";
                           str += "手机号码: <code>" +row.phone_text + "</code><br>";
                           str += "真实姓名: <code>" +row.name_text + "</code><br>";
                           str += "专属账号: <code>" +row.nb_text + "</code>";
                        }
                        Layer.alert(str);
                    }
                },
               send: {
                 //格式为：方法名+空格+DOM元素
                 'click .btn-gift': function (e, value, row, index) {
                   e.stopPropagation();
                   if(row.send > 0)
                   {
                     layer.open({
                       type: 1,
                       skin: 'layui-layer-demo', //样式类名
                       title:'短信记录',
                       anim: 2,
                       shadeClose: true, //开启遮罩关闭
                       area: ['50%', '50%'],
                       content: row.send_list
                     });
                     return false;
                   }
                   Layer.alert('还没有短信记录。');
                 }
               }
            }
        }
    };
    return Controller;
});